<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input v-model="form.account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model="form.mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model="form.code" class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click="send">{{ countTime === 0 ? '发送验证码' : countTime + '秒后重试'}}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.rePassword" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
  </div>
</template>

<script>
import { reactive } from 'vue'
import { useCountDown } from '@/compositions/index'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'
import { message } from '@/components/Message'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String
    }

  },
  setup (props) {
    // 表单数据
    const form = reactive({
      account: '',
      mobile: '',
      code: '',
      password: '',
      rePassword: ''
    })
    // 发送验证码
    const sjh = /^1[3-9]\d{9}$/
    const { countTime, start } = useCountDown(60)
    const send = async () => {
      if (countTime.value === 0 || sjh.test(form.mobile)) {
        await reqUserQQPatchCode(form.mobile)
        message({ type: 'success', text: '发送验证码' })
        start()
      }
    }
    const router = useRouter()
    // 点击提交按钮
    const submit = async () => {
      // 密码校验
      if (form.password !== form.rePassword) return message({ text: '两次密码不一致' })
      const { result } = await reqUserQQPatchLogin({
        unionId: props.unionId,
        ...form
      })
      console.log(84, result)
      // 注册完成跳转登录页去登录
      router.push('/login')
    }
    return { form, send, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
